/*
* Copyright 2007-2008 WorldWide Conferencing, LLC
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing,
* software distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions
* and limitations under the License.
*/

package cl.fbd.widget.treetable

import scala.xml.{NodeSeq, Node, PCData, Text, Unparsed}

import cl.fbd.widget.model.{TreeModel,ColumnExtractor}

import net.liftweb.http.S

/*
 * 
 */
 
class TreeTableOptions {
  val showRoot : Boolean = false
  val collapseUnderLevel : Option [Int] = None  // None => don't collapse
  val striped : Boolean = false
  val highlight: Boolean = true
  val column: Int = 0
  val onselect : Option [String] = None
}

/*
 * 
 */
 
object TreeTable {
  /**
   * register the resources with lift (typically in boot)
   */
   
  def init() {  
    import net.liftweb.http.ResourceServer  
   
    ResourceServer.allow({  
      case "treetable" :: _ => true  
    })
    
    println("register tree-table")  
  }
  
  /**
   * render a tree-table using this jQuery plugin: http://www.hanpau.com/jquery/unobtrusivetreetable.php
   */
  
  def render [A, B] (tm : TreeModel [A, B], ce : ColumnExtractor, options : TreeTableOptions) : NodeSeq = {
    
    val ttg : TreeTableGenerator = new TreeTableGenerator (tm, "tablemain", "treetable", ce, options.showRoot) 
                                          
    val js = renderJs (ttg, options)
    
      <xml:group>
      <head>
        <link href="/classpath/treetable/themes/default/jQTreeTable.css" type="text/css" rel="stylesheet" />
        <script type="text/javascript" src="/classpath/treetable/jQTreeTable.js"></script>
        
        <script type="text/javascript">
        {Unparsed (js)}
        </script>
      </head>
      
      {ttg.generateXhtml}
      </xml:group>
  }
  
  /*
   *
   */
  
  private def renderJs (ttg : TreeTableGenerator, options : TreeTableOptions) : String = {
    
    var sz = new StringBuilder (500)
    
    sz.append ( """
/* <![CDATA[ */		      
jQuery(function(){
  var map = [ 
  """)
  
  sz.append (renderJsMap (ttg))
  
  sz.append ("""
    ] ;
    var options = {
"""
  )
  
  sz.append ("      openImg: \""     + S.contextPath + "/classpath/treetable/themes/default/tv-collapsable.gif\",\n")  
  sz.append ("      shutImg: \""     + S.contextPath + "/classpath/treetable/themes/default/tv-expandable.gif\",\n")  
  sz.append ("      leafImg: \""     + S.contextPath + "/classpath/treetable/themes/default/tv-item.gif\",\n")
  sz.append ("      lastOpenImg: \"" + S.contextPath + "/classpath/treetable/themes/default/tv-collapsable-last.gif\",\n") 
  sz.append ("      lastShutImg: \"" + S.contextPath + "/classpath/treetable/themes/default/tv-expandable-last.gif\",\n")
  sz.append ("      lastLeafImg: \"" + S.contextPath + "/classpath/treetable/themes/default/tv-item-last.gif\",\n")
  sz.append ("      vertLineImg: \"" + S.contextPath + "/classpath/treetable/themes/default/vertline.gif\",\n")
  sz.append ("      blankImg: \""    + S.contextPath + "/classpath/treetable/themes/default/blank.gif\",\n")
  
  // 
  options.collapseUnderLevel match {
    case None => sz.append ("      collapse: " + ttg.generateCollapse () + ",\n")
    case Some(level) => sz.append ("      collapse: " + ttg.generateCollapse (level) + ",\n")
  }
  
  sz.append ("      column: " + options.column + ",\n")        
  sz.append ("      striped: " + options.striped + ",\n")
  
  options.onselect match {
    case Some (function) => sz.append ("      onselect: " + function + ",\n")
    case None => 
  }
  
  sz.append ("      highlight: " + options.highlight + "\n")
      
  sz.append ("""  
    };
    jQuery("#treetable").jqTreeTable(map, options);
});
/* ]]> */    
    """)
    
    sz.toString
  }
  
  private def renderJsMap (ttg : TreeTableGenerator) : String = {
    ttg.generateMap ()
  }
  
}
